#!/bin/bash
typeid=1
host='127.0.0.1'
db='recommendationengine'
idFile=shouyin_kandapian_reju.csv
WEIGHT_UNIT=1
needTruncate=false

hinit() {
    rm -f /tmp/hashmap.$1
}
hput() {
    echo "$2 $3" >> /tmp/hashmap.$1
}
hget() {
    grep "^$2 " /tmp/hashmap.$1 | awk '{ print $2 };'
}

sed 's/ //g' $idFile | tr -d "\r" > $idFile.clean
ids=''
hinit idWeightMap
while read line
do
    id=`echo $line | cut -d',' -f3`
    weight=`echo $line | cut -d',' -f1`
    weight=${weight%.*} # remove tailing decimal
    weight=$((weight / WEIGHT_UNIT))
    if [ $weight -lt 1 ]; then weight=1; fi
    hput idWeightMap $id $weight
    ids=$ids\'$id\',
done < $idFile.clean
ids=${ids%','} ## remove the tailing comma

charset='--default-character-set=utf8'
fetchLeftIdSQL="select seriesid into outfile '/tmp/left_id' from series where typeid=$typeid"
fetchRightIdSQL="select seriesid into outfile '/tmp/right_id' from (select seriesid from series as seriesid where seriesid in ($ids) union select programid as seriesid from program where programid in ($ids)) t"

date
echo '[info] start job' 
rm -f /tmp/left_id 
rm -f /tmp/right_id
echo '[info] fetching left IDs into /tmp/left_id...'
mysql -uroot -pmysql -h$host $db -e "$fetchLeftIdSQL"
echo '[info] fetching right IDs into /tmp/right_id...'
mysql -uroot -pmysql -h$host $db $charset -e"$fetchRightIdSQL"
echo '[info] reading left ID...'
i=0
while read line
do
    leftIdArr[i]=$line
    i=$((i+1))
done < /tmp/left_id
echo "[info] totoal number of left ID fetched: ${#leftIdArr[@]}"

echo '[info] reading right ID...'
i=0
while read line
do
    weight=`hget idWeightMap $line`
    for ((j=0; j<$weight; j++))
    do 
        n=$((i+j))
        rightIdArr[n]=$line
    done
    i=$((i + j))
done < /tmp/right_id
echo "[info] totoal number of right ID fetched: ${#rightIdArr[@]}"

echo ${rightIdArr[@]}

if $needTruncate
then
    echo "[info] truncating manualseries table..."
    mysql -uroot -pmysql -h$host $db -e"truncate table manualseries"
fi

echo '[info] start to insert manual recommendation rules RANDOMLY...'
i=0
mod=${#rightIdArr[@]}
for leftId in ${leftIdArr[@]}
do
    index=$(($RANDOM % $mod))
    echo '****************************************************************'
    echo "[info] current i:$i, index:$index"
    echo '****************************************************************'
    rightId=${rightIdArr[index]}
    echo "[info] deleting manual recommendation rules for left ID: $leftId"
    mysql -uroot -pmysql -h$host $db -e"delete from manualseries where leftseries='$leftId'"
    echo "[info] inserting manual recommendation for left ID: $leftId, and right ID: $rightId"
    mysql -uroot -pmysql -h$host $db -e"insert into manualseries values(0, '$leftId', '$rightId', 1)"
    i=$((i + 1))
done

echo '[info] exporting result...'
mysql -uroot -pmysql -h$host $db $charset -e"select * from (select p.seriesid, p.name, count(1) cnt, p.typeid from manualseries mp, series p where mp.rightseries=p.seriesid group by mp.rightseries union select p.programid, p.name, count(1) cnt, p.typeid from manualseries mp, program p where mp.rightseries=p.programid group by mp.rightseries) t order by typeid, cnt desc"

echo '[info] Job is done!!! Please check manualseries table for verification...' 
date
